#include "pj_file.hpp"

/*
 * write string to file
 * para: const string& fileName, const string content
 * return: pair<bool, string>: code, content; code true, or code false with error info
 */
pair<bool, string> WriteStringTofile(const string& fileName, const string content){
	/****************************open and write string*****************************/
    fstream s(fileName, s.out|s.trunc);    //truncate the old content
    if (!s.is_open()) {
        return pair<bool, string>{false, (string)"WriteStringTofile: cannot open target file for write!, filename is:" + fileName};
    } else {
        s << content;
        s.close();
        return pair<bool, string>{true, ""};
    }
}

/*
 * read data content to string
 * input: const string& fileName
 * return pair<bool, string>: code, content; code true with file content, or code false with error info
 */
pair<bool, string> ReadFileToString(const string& fileName)
{
    fstream s(fileName, s.in);
    if (!s.is_open()) {
        return {false, "ReadFileToString: cannot find " + fileName};
    } else {
        ostringstream tmp;
        tmp << s.rdbuf();   
        s.close();
        return {true, tmp.str()};
    }
}
